package com.yunze.system.card;

import com.alibaba.fastjson.JSON;
import com.yunze.common.mapper.mysql.YzCardMapper;
import com.yunze.common.mapper.mysql.YzExecutionTaskMapper;
import com.yunze.common.mapper.mysql.warehouse.Administration.YzCkglInventorydetailsMapper;
import com.yunze.common.utils.yunze.WriteCSV;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * 导入至卡信息 MQ
 */
@Slf4j
@Component
public class ImportCard {

    @Resource
    private WriteCSV writeCSV;
    @Resource
    private YzExecutionTaskMapper yzExecutionTaskMapper;
    @Resource
    private YzCkglInventorydetailsMapper yzCkglInventorydetailsMapper;
    @Resource
    private YzCardMapper yzCardMapper;

    private String Outcolumns[] = {"执行总数", "成功总数", "失败总数", "执行描述", "执行人", "执行结果"};
    private String keys[] = {"COUNT", "scsCon", "filCon", "describe", "agentName", "result"};
    private int OutSize = 50;//每 50条数据输出一次

    /**
     * 导入至卡信息
     *
     * @param msg
     * @throws IOException
     */

    @RabbitHandler
    @RabbitListener(queues = "admin_ImportCard_queue")
    private void SetCardInfo(String msg) throws IOException {
        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String, Object> map = JSON.parseObject(msg);
            Map<String, Object> Rmap = (Map<String, Object>) map.get("map");//参数
            Map<String, Object> User = (Map<String, Object>) map.get("User");//登录用户信息

            executionel(Rmap, User);
        } catch (Exception e) {
            log.error(">>错误 - 特殊操作 导入至卡信息 :{}<<", e.getMessage().toString());
        }
    }

    private void executionel(Map<String, Object> Rmap, Map<String, Object> User) {


        Map<String, String> Dept = (Map<String, String>) User.get("dept");
        String create_by = " [ " + Dept.get("deptName") + " ] - " + " [ " + User.get("userName") + " ] ";
        String newName = UUID.randomUUID().toString().replace("-", "") + "_FleCard";//对应 执行任务导出类别 CSV 前缀
        String failAdd = UUID.randomUUID().toString().replace("-", "") + "_failCard";
        String task_name = "导入 [" + "至卡信息" + "] ";
        String SaveUrl = "/getcsv/" + newName + ".csv";
        String Url = "";
        SaveUrl = "/getcsv/" + newName + ".csv";
        Url += SaveUrl + ",/getcsv/" + failAdd + ".csv"; // 进行追加
        Map<String, Object> task_map = new HashMap<String, Object>();
        task_map.put("auth", create_by);
        task_map.put("task_name", task_name);
        task_map.put("url", SaveUrl);
        task_map.put("agent_id", Dept.get("deptId"));
        task_map.put("type", "31"); //对应 字典表的 任务类别
        yzExecutionTaskMapper.add(task_map);//添加执行 任务表

        List<Map<String, Object>> list = yzCkglInventorydetailsMapper.repeatIdArr(Rmap);

        Integer COUNT = yzCkglInventorydetailsMapper.MapCount(Rmap);//本次执行总数
        Integer wCOUNT = yzCkglInventorydetailsMapper.wCOUNT(Rmap);//不符合导入条件的
        List<Map<String, Object>> AddArr = new ArrayList<>();

        if (COUNT != null && COUNT > 0) {
            try {
                Integer subtraction = COUNT - wCOUNT; //执行总数 减去 不符合条件的
                Integer total = subtraction - list.size();

                if (total > 0) {//成功
                    // 执行成功 插入数据
                    if (wCOUNT > 0) {
                        partFail(AddArr, COUNT, subtraction, wCOUNT, create_by);
                    } else {
                        successArr(AddArr, COUNT, subtraction, wCOUNT, create_by);
                    }
                    Rmap.put("iccid_Arr", list);
                    Integer ImportAddCard = yzCkglInventorydetailsMapper.ImportAddCard(Rmap);
                    System.out.println(ImportAddCard);
                    log.info(" 导入至卡信息 插入 ： " + ImportAddCard + " 成功");
                }else {
                    if(COUNT>0){
                        failCard(AddArr, COUNT, total, subtraction, create_by);
                    }
                    if(wCOUNT>0){
                        failCard2(AddArr, COUNT, total, wCOUNT, create_by);
                    }
                }
                writeCSV.OutCSVObj(AddArr, newName, Outcolumns, keys, null, OutSize); //添加cvs
            } catch (Exception e) {
                log.error(">> 执行数据异常  :{}<<", e.getMessage());
            }
            yzExecutionTaskMapper.set_end_time(task_map);//任务结束
        }
    }

    /**
     * 全部成功
     */
    public void successArr(List<Map<String, Object>> AddArr, Integer COUNT, Integer subtraction, Integer wCOUNT, String create_by) {
        Map<String, Object> Sucs_Arr = new HashMap<>();
        Sucs_Arr.put("COUNT", COUNT);
        Sucs_Arr.put("scsCon", subtraction);
        Sucs_Arr.put("filCon", wCOUNT);
        Sucs_Arr.put("describe", "全部成功");
        Sucs_Arr.put("agentName", create_by);
        Sucs_Arr.put("result", "全部成功！");
        AddArr.add(Sucs_Arr);
    }


    /**
     * 部分成功
     */
    public void partFail(List<Map<String, Object>> AddArr, Integer COUNT, Integer subtraction, Integer wCOUNT, String create_by) {
        Map<String, Object> Fail_f = new HashMap<>();
        Fail_f.put("COUNT", COUNT);
        Fail_f.put("scsCon", subtraction);
        Fail_f.put("filCon", wCOUNT);
        Fail_f.put("describe", "不符合导入条件");
        Fail_f.put("agentName", create_by);
        Fail_f.put("result", "部分成功");
        AddArr.add(Fail_f);
    }

    /**
     * 全部执行失败
     */
    public void failCard(List<Map<String, Object>> AddArr, Integer COUNT, Integer total, Integer subtraction, String create_by) {
        Map<String, Object> Fail_Map = new HashMap<>();
        Fail_Map.put("COUNT", COUNT);
        Fail_Map.put("scsCon", total);
        Fail_Map.put("filCon", subtraction);
        Fail_Map.put("describe", "ICCID重复");
        Fail_Map.put("agentName", create_by);
        Fail_Map.put("result", "执行失败！");
        AddArr.add(Fail_Map);
    }

    /**
     * 执行失败
     */
    public void failCard2(List<Map<String, Object>> AddArr, Integer COUNT, Integer total, Integer wCOUNT, String create_by) {
        Map<String, Object> Fail_Map = new HashMap<>();
        Fail_Map.put("COUNT", COUNT);
        Fail_Map.put("scsCon", total);
        Fail_Map.put("filCon", wCOUNT);
        Fail_Map.put("describe", "不符合导入条件");
        Fail_Map.put("agentName", create_by);
        Fail_Map.put("result", "执行失败！");
        AddArr.add(Fail_Map);
    }
}








































